import { ref } from 'vue'
import { defineStore } from 'pinia'
import api from '@/api/order'
import cfg from '@/config/config'
import type { PageConfig, OrderItem, OrderOperateItem, OptionItem } from '@/config/types'
import type { OrderSearchForm } from '@/config/forms'

export const useOrderStore = defineStore('order', () => {
    const loading = ref<boolean>(false)
    const orders = ref<OrderItem[]>([])
    const searchForm = ref<OrderSearchForm>({
        orderNo: '',
        studentName: '',
        lessonTitle: '',
        status: 1,
        minPrice: 0,
        maxPrice: 0,
        startDate: '',
        endDate: '',
        page: 1,
        limit: cfg.pageLimit
    })
    const statusOptions = ref<OptionItem[]>([
        {value: 1, text: '待支付'},
        {value: 2, text: '已完成'},
        {value: 0, text: '已取消'}
    ])
    const pager = ref<PageConfig>({
        page: 1,
        pageSize: 10,
        total: 0
    })
    const orderItem = ref<OrderItem | null>(null)
    const operators = ref<OrderOperateItem[]>([])

    async function loadOrders() {
        loading.value = true
        try {
            const res = await api.searchList(searchForm.value)
            orders.value = res.data
            pager.value = {
                page: res.page,
                pageSize: res.size,
                total: res.total
            }
        } finally {
            loading.value = false
        }
    }

    function searchOrders() {
        searchForm.value.page = 1
        loadOrders()
    }

    function changePage(page: number) {
        searchForm.value.page = page
        loadOrders()
    }

    async function getOrder(id: number) {
        loading.value = true
        try {
            const res = await api.getItem(id)
            orderItem.value = res.data
        } finally {
            loading.value = false
        }
    }

    async function changePrice(id: number, price: number) {
        loading.value = true
        let result = false
        try {
            const res = await api.changePrice(id, price)
            result = res.data
        } finally {
            loading.value = false
        }
        return result
    }

    async function complete(id: number) {
        loading.value = true
        let result = false
        try {
            const res = await api.complete(id)
            result = res.data
        } finally {
            loading.value = false
        }
        return result
    }

    async function cancel(id: number) {
        loading.value = true
        let result = false
        try {
            const res = await api.cancel(id)
            result = res.data
        } finally {
            loading.value = false
        }
        return result
    }

    async function getOperators(id: number) {
        loading.value = true
        try {
            const res = await api.operators(id)
            operators.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        orders,
        searchForm,
        statusOptions,
        pager,
        orderItem,
        operators,

        searchOrders,
        changePage,
        getOrder,
        changePrice,
        complete,
        cancel,
        getOperators
    }
})